{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Custom Output Parsers 自定义输出分析器\n",
    "\n",
    "> In some situations you may want to implement a custom parser to structure the model output into a custom format.<br>\n",
    "在某些情况下，您可能希望实现自定义分析器，以将模型输出结构化为自定义格式。\n",
    "\n",
    "There are two ways to implement a custom parser:<br>\n",
    "有两种方法可以实现自定义分析器：\n",
    "\n",
    "* Using RunnableLambda or RunnableGenerator in LCEL -- we strongly recommend this for most use cases<br>\n",
    "在 LCEL 中使用 RunnableLambda 或 RunnableGenerator -- 我们强烈建议在大多数用例中使用\n",
    "* By inherting from one of the base classes for out parsing -- this is the hard way of doing things<br>\n",
    "通过从其中一个基类中继承出来进行解析 - 这是很难的做事方式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Runnable Lambdas and Generators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hELLO! hOW CAN i ASSIST YOU TODAY?'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from typing import Iterable\n",
    "\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.messages import AIMessage, AIMessageChunk\n",
    "\n",
    "model = ChatOpenAI()\n",
    "\n",
    "\n",
    "def parse(ai_message: AIMessage) -> str:\n",
    "    \"\"\"Parse the AI message.\"\"\"\n",
    "    return ai_message.content.swapcase()\n",
    "\n",
    "\n",
    "chain = model | parse\n",
    "chain.invoke(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i AM A DEDICATED AND HARDWORKING INDIVIDUAL WHO IS PASSIONATE ABOUT LEARNING AND GROWING IN ALL ASPECTS OF LIFE.|"
     ]
    }
   ],
   "source": [
    "for chunk in chain.stream(\"tell me about yourself in one sentence\"):\n",
    "    print(chunk, end=\"|\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hELLO! hOW CAN i ASSIST YOU TODAY?'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.runnables import RunnableGenerator\n",
    "\n",
    "\n",
    "def streaming_parse(chunks: Iterable[AIMessageChunk]) -> Iterable[str]:\n",
    "    for chunk in chunks:\n",
    "        yield chunk.content.swapcase()\n",
    "\n",
    "\n",
    "streaming_parse = RunnableGenerator(streaming_parse)\n",
    "\n",
    "chain = model | streaming_parse\n",
    "chain.invoke(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|i| AM| A| DEDICATED| AND| HARD|WORKING| INDIVIDUAL| WHO| IS| ALWAYS| LOOKING| TO| LEARN| AND| GROW| IN| BOTH| MY| PERSONAL| AND| PROFESSIONAL| LIFE|.||"
     ]
    }
   ],
   "source": [
    "for chunk in chain.stream(\"tell me about yourself in one sentence\"):\n",
    "    print(chunk, end=\"|\", flush=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain0_1",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
